<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改拜访日志')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-visit_log-edit" th:object="${wbVisitLog}">
        <input id="id" name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">用户类型：</label>
            <div class="col-sm-8">
                <select class="form-control m-b" name="communicationOperator"
                        onchange="setExpiredTimeSelectRequired(this.value)"
                        th:with="type=${@dict.getType('communication_operators')}"
                        required>
                    <option th:each="dict : ${type}" th:field="*{communicationOperator}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">用户姓名：</label>
            <div class="col-sm-8">
                <input class="form-control" name="userName" required th:field="*{userName}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">当前号码：</label>
            <div class="col-sm-8">
                <input class="form-control" name="currentPhoneNumber" required th:field="*{currentPhoneNumber}"
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">套餐价值：</label>
            <div class="col-sm-8">
                <input class="form-control" name="mealValue" required th:field="*{mealValue}" type="text">
            </div>
        </div>
        <div class="form-group" id="expiredTimeDiv">
            <label class="col-sm-3 control-label" id="expiredTimeLabel">到期时间：</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <input class="form-control time-input" id="expiredTimeInput" name="expiredTime"
                           placeholder="yyyy-MM-dd"
                           th:value="${#dates.format(wbVisitLog.expiredTime, 'yyyy-MM-dd')}" type="text">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">转化号码：</label>
            <div class="col-sm-8">
                <input class="form-control" name="translatedPhoneNumber" th:field="*{translatedPhoneNumber}"
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">现场照片：</label>
            <div class="col-sm-8">
                <input id="scenePhotoId" name="scenePhoto" th:field="*{scenePhoto}" type="hidden">
                <div class="file-loading">
                    <input accept="image/*" class="form-control file-upload" id="scenePhoto" name="file"
                           onclick="judgeStatus()"
                           type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea class="form-control" name="remake">[[*{remake}]]</textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    let maxImageCount = 5;
    var prefix = ctx + "workbench/visit_log";
    $("#form-visit_log-edit").validate({
        focusCleanup: true,
        onkeyup: false,
        rules: {
            currentPhoneNumber: {
                isPhone: true,
                remote: {
                    url: prefix + "/checkPhoneNumberUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        id:function () {
                            return $.common.trim($("#id").val());
                        },
                        type: '',
                        phoneNumber: function () {
                            return $.common.trim($("#currentPhoneNumber").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            translatedPhoneNumber: {
                isPhone: true,
                remote: {
                    url: prefix + "/checkPhoneNumberUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        id:function () {
                            return $.common.trim($("#id").val());
                        },
                        type: '1',
                        phoneNumber: function () {
                            return $.common.trim($("#translatedPhoneNumber").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            }
        },
        messages: {
            "currentPhoneNumber": {
                remote: "手机号码已经录入"
            },
            "translatedPhoneNumber": {
                remote: "手机号码已经转化"
            },
        }
    });

    /**
     * 设置过期时间是否必填，异网用户必填，本网用户和潜在用户可填可不填
     */
    function setExpiredTimeSelectRequired(value) {
        let expiredTimeInput = document.getElementById('expiredTimeInput')
        if (value === '6') {
            $('#expiredTimeLabel').addClass('is-required')
            expiredTimeInput.setAttribute('required', 'required')
        } else {
            $('#expiredTimeLabel').removeClass('is-required')
            expiredTimeInput.removeAttribute('required')
        }
        $("#expiredTimeDiv").selectpicker('refresh')
    }

    function submitHandler() {
        if ($('#currentPhoneNumber').val() === $('#translatedPhoneNumber').val()) {
            $.modal.alertError('转化号码不能与当前号码相同');
            return
        }
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-visit_log-edit').serialize());
        }
    }

    function getImageCount() {
        return $(".file-preview-thumbnails").find('.file-preview-frame').length / 2;
    }

    function judgeStatus() {
        if (getImageCount() >= maxImageCount) {
            $('#scenePhoto').attr('disabled', 'disabled');
            $.modal.msg("图片允许上传的最大数值为" + maxImageCount + "，请删除需要被替换的图片后再进行上传操作!");
        }
    }

    let scenePhotoInputer = $("#scenePhotoId");
    let imageArray = scenePhotoInputer.val().split(',');
    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload',
        initialPreviewAsData: true,
        initialPreview: imageArray,
        overwriteInitial: false,
        allowedPreviewTypes: ['image'],
        allowedFileTypes: ['image']
    }).on('fileuploaded', function (event, data, previewId, index) {
        let images = scenePhotoInputer.val();
        if (images === '') {
            images = data.response.url;
        } else {
            images = images + ',' + data.response.url;
        }
        scenePhotoInputer.val(images)
    }).on("filebatchselected", function (event, data, previewId, index) {
        // 文件选择完直接调用上传方法。
        $(this).fileinput("upload");
    }).on('fileremoved', function (event, id, index) {
        let images = scenePhotoInputer.val();
        let imageArray = images.split(',');
        imageArray.splice(index.split('_')[1], 1);
        scenePhotoInputer.val(imageArray.toString());
        if (getImageCount() < maxImageCount) {
            $('#scenePhoto').removeAttr('disabled');
        }
    }).fileinput('_initFileActions');
</script>
</body>
</html>
